#pragma Once
#include <iostream>
#include "Protocol.hpp"

enum
{
    Div_Zero = 1,
    Mod_Zero,
    Other_Oper
};

class ServerCal
{
public:
    Response Headler(Request &req)
    {
        Response res(0, 0);
        switch (req.op_)
        {
        case '+':
            res.result_ = req.x_ + req.y_;
            break;
        case '-':
            res.result_ = req.x_ - req.y_;
            break;
        case '*':
            res.result_ = req.x_ * req.y_;
            break;
        case '/':
        {
            if (req.y_ == 0)
                res.exitcode_ = Div_Zero;
            else
                res.result_ = req.x_ / req.y_;
        }
        break;
        case '%':
        {
            if (req.y_ == 0)
                res.exitcode_ = Mod_Zero;
            else
                res.result_ = req.x_ % req.y_;
        }
        break;
        default:
            res.exitcode_ = Other_Oper;
            break;
        }

        return res;
    }
    std::string Calculator(std::string &package) // 将报文处理成我们想要的结构
    {
        std::string content;
        bool n = Decode(package, &content);
        if (!n)
            return ""; // Decode 出现问题返回一个空字符串

        Request req;
        req.Deserialize(content); // 将 x op y 反序列化成 对象。

        Response res = Headler(req); // 处理 request 对象，将 数据转换成 response对象

        content = "";
        res.Serialize(&content);   // 将 response 序列化成字符串
        content = Encode(content); // "len\n"result exitcode

        return content;
    }
};